package top.chmin.project.cloud.builder;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumn;
import top.chmin.project.cloud.common.entity.label.value.auto.EntityLabelValue;

import java.util.stream.Collectors;

/**
 * chmin
 * 2023/12/15 15:21
 * cloud-builder
 */
@Component
public class SelectBuilder implements Builder {
    @Override
    public void build(BuilderConfig config) {
        if (CollectionUtil.isEmpty(config.getEntityLabelValueList())) {
            return;
        }
        for (EntityLabelValue entityLabelValue : config.getEntityLabelValueList()) {
            if (!"SELECT".equals(entityLabelValue.getDataApiType())) {
                continue;
            }
            EntityColumn label = config.getEntityColumnList()
                    .stream().filter(f -> f.getFieldCode().equals(entityLabelValue.getLabelFieldCode()))
                    .findFirst().orElse(null);
            EntityColumn value = config.getEntityColumnList()
                    .stream().filter(f -> f.getFieldCode().equals(entityLabelValue.getValueFieldCode()))
                    .findFirst().orElse(null);
            if (label == null || value == null) {
                continue;
            }

            String content = """
                    package top.chmin.project.cloud.#e.module#.#e.pack#.auto;

                    #import#

                    /**
                     * chmin
                     * #dateTime#
                     * cloud-builder
                     */
                    public interface #e.simpleName#%sSelect {

                        @PostMapping("/au/#e.fieldName#/%s/select")
                        List<Result> select%s(@RequestBody Param param);

                        @Getter
                        @Setter
                        @Accessors(chain = true)
                        class Param {
                    %s
                    %s
                        }

                        @Getter
                        @Setter
                        @Accessors(chain = true)
                        class Result {
                    %s
                            String label;
                            %s value;
                        }

                    }
                    """.formatted(
                    value.getMethodName(),
                    value.getFieldName(),
                    value.getMethodName(),
                    config.getEntityColumnList().stream()
                            .filter(f -> !"LocalDateTime".equals(f.getSimpleType()))
                            .map(f -> fixEntityColumn(f, "        // #f.remark#\n        #f.simpleType# #f.fieldName#;"))
                            .collect(Collectors.joining("\n")),
                    config.getEntityColumnList().stream()
                            .filter(f -> "LocalDateTime".equals(f.getSimpleType()))
                            .map(f -> fixEntityColumn(f, "        // #f.remark#\n        List<#f.simpleType#> #f.fieldName#;"))
                            .collect(Collectors.joining("\n")),
                    config.getEntityColumnList().stream()
                            .map(f -> fixEntityColumn(f, "        // #f.remark#\n        #f.simpleType# #f.fieldName#;"))
                            .collect(Collectors.joining("\n")),
                    value.getSimpleType()
            );
            save(config, config.getEntity().getClassName() + value.getMethodName() + "Select", content);
        }
    }
}
